
--[[

    slimui - Copyright (c) 2011 atom0s, All rights reserved.

    See Copyright notice in slimui.lua
    
]]--

class 'MenuItem: Component' { ... };

----------------------------------------------------------------------------------
-- func: MenuItem.__init__
-- desc: Constructor called when this object is first created.
----------------------------------------------------------------------------------
function MenuItem.__init__( self )
    Component.__init__( self );
end

----------------------------------------------------------------------------------
-- func: MenuItem:Create
-- desc: Creates a new menuitem object.
----------------------------------------------------------------------------------
function MenuItem:Create( parent )
    if parent == nil or parent._object == nil then
        slimui:err( 'MenuItem:Create - parent must be set to create a control!' );
    end

    self._object = createMenuItem( parent._object );

    if self._object == nil then
        slimui:err( 'MenuItem:Create - failed to initialize a new object.' );
    end

    local class = object_getClassName( parent._object );
    if string.lower( class ) == 'tmainmenu' or string.lower( class ) == 'tpopupmenu' then

        -- Add item to main menu object.
        local items = menu_getItems( parent._object );
        menuItem_add( items, self._object );
    
    elseif string.lower( class ) == 'tmenuitem' then

        -- Add item to submenu item object.
        menuItem_add( parent._object, self._object );

    else
        slimui:err( 'MenuItem:Create - invalid parent for menuitem! - ' .. class );
    end

    return self;
end

----------------------------------------------------------------------------------
-- func: MenuItem:Bind
-- desc: Binds this menuitem object to the given menuitem.
----------------------------------------------------------------------------------
function MenuItem:Bind( menu )
    if menu == nil then
        slimui:err( 'MenuItem:Bind - parent menu cannot be nil!' );
    end

    local class = object_getClassName( menu );
    if string.lower( class ) ~= 'tmenuitem' then
        slimui:err( 'MenuItem:Bind - cannot bind to objects that are not a menuitem!' );
    end

    if self._object ~= nil then
        object_destroy( self._object );
    end

    self._object = menu;

    return self;
end

----------------------------------------------------------------------------------
-- func: MenuItem:GetCaption
-- desc: Returns the caption of this menuitem object.
----------------------------------------------------------------------------------
function MenuItem:GetCaption( )
    return menuItem_getCaption( self._object );
end

----------------------------------------------------------------------------------
-- func: MenuItem:SetCaption
-- desc: Sets the caption of this menuitem object.
----------------------------------------------------------------------------------
function MenuItem:SetCaption( caption )
    menuItem_setCaption( self._object, caption );
end

----------------------------------------------------------------------------------
-- func: MenuItem:GetShortcut
-- desc: Returns the shortcut of this menuitem object.
----------------------------------------------------------------------------------
function MenuItem:GetShortcut( )
    return menuItem_getShortcut( self._object );
end

----------------------------------------------------------------------------------
-- func: MenuItem:SetShortcut
-- desc: Sets the shortcut of this menuitem object.
----------------------------------------------------------------------------------
function MenuItem:SetShortcut( shortcut )
    menuItem_setShortcut( self._object, shortcut );
end

----------------------------------------------------------------------------------
-- func: MenuItem:GetCount
-- desc: Returns the item count of this menuitem object.
----------------------------------------------------------------------------------
function MenuItem:GetCount( )
    return menuItem_getCount( self._object );
end

----------------------------------------------------------------------------------
-- func: MenuItem:Add
-- desc: Adds a subitem to this menuitem object.
----------------------------------------------------------------------------------
function MenuItem:Add( item )
    local class = object_getClassName( item );
    if string.lower( class ) == 'tmenuitem' then
        menuItem_add( self._object, item );
    end
end

----------------------------------------------------------------------------------
-- func: MenuItem:Delete
-- desc: Deletes the menuitem at the given index.
----------------------------------------------------------------------------------
function MenuItem:Delete( index )
    menuItem_delete( self._object, index );
end

----------------------------------------------------------------------------------
-- func: MenuItem:OnClick
-- desc: Sets the onclick event handler for this menuitem object.
----------------------------------------------------------------------------------
function MenuItem:OnClick( func )
    menuItem_onClick( self._object, func );
end



--[[ Debug information. ]]--------------------------------------------------------
if ( slimui:isdebug() == true ) then
    slimui:msg( '(Control) menuitem.lua successfully loaded.' );
end